package edu.ucsd.cse110;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Sets up the Logbook, Home, and Benchmark screens.
 */
public class XFitTracker extends FragmentActivity {
	/**
	 * Adapter class that populates pages for ViewPager.
	 */
	private class PagerAdapter extends FragmentPagerAdapter {
		/**
		 * Stores the fragment of the screens.
		 */
		private List<Fragment> mFragments;

		/**
		 * Constructs the adapter with a list of pages.
		 */
		public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
			super(fm);
			mFragments = fragments;
		}

		/**
		 * Returns the number of fragments in this adapter.
		 */
		@Override
		public int getCount() {
			return mFragments.size();
		}

		/**
		 * Returns the fragment at a specific position
		 */
		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position);
		}
	}

	private static final int REQUEST_CODE_DETAIL = 20;
	public static final String PREFS_NAME = "MyPrefsFile";

	private int mBackground;
	protected static ViewPager mPager;
	private PagerAdapter mPagerAdapter;
	private static final int INITIAL_PAGE = 1;
	private static final int OFFSCREEN_LIMIT = 2;

	/**
	 * Initializes the fragment of the three screens.
	 */
	private void initializePaging() {
		// Creates a list of pages.
		List<Fragment> fragments = new Vector<Fragment>();
		fragments.add(Fragment.instantiate(this,
				LogbookFragment.class.getName()));
		fragments.add(Fragment.instantiate(this, HomeFragment.class.getName()));
		fragments.add(Fragment.instantiate(this,
				BenchmarkFragment.class.getName()));

		// Initializes pager adapter.
		mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);

		// Sets the view pager to mPagerAdapter.
		mPager = (ViewPager) findViewById(R.id.main_viewpager);
		mPager.setOffscreenPageLimit(OFFSCREEN_LIMIT);
		mPager.setAdapter(mPagerAdapter);

		// Sets the default page to home.
		mPager.setCurrentItem(INITIAL_PAGE);
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REQUEST_CODE_DETAIL) {
			if (resultCode == Activity.RESULT_OK) {
				mPagerAdapter.getItem(2).onActivityResult(requestCode,
						resultCode, data);
			}
		}
	}

	/**
	 * Exits the program when the back button is pressed on the home screen.
	 * Otherwise scrolls back to the home screen.
	 */
	@Override
	public void onBackPressed() {
		if (mPager.getCurrentItem() != INITIAL_PAGE) {
			mPager.setCurrentItem(INITIAL_PAGE);
		} else {
			super.onBackPressed();
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initializePaging();

		String name = SettingsActivity.getName(this);
		if (!name.equals("")) {
			Toast.makeText(this, "Welcome back " + name, 40000).show();
		}

		SharedPreferences settings = getPreferences(0);
		mBackground = settings.getInt("background", 0);

		LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);

		// Handle item selection
		switch (mBackground) {
		case R.id.main_menu_none:
			layout.setBackgroundResource(0);
			break;
		case R.id.main_menu_black:
			layout.setBackgroundResource(R.drawable.home_background_port_bw);
			break;
		case R.id.main_menu_blue:
			layout.setBackgroundResource(R.drawable.home_background_port);
			break;
		case R.id.main_menu_brushed:
			layout.setBackgroundResource(R.drawable.home_background);
			break;
		case R.id.main_menu_pink:
			layout.setBackgroundResource(R.drawable.home_background_pink);
			break;
		case R.id.main_menu_gold:
			layout.setBackgroundResource(R.drawable.home_background_port_gold);
			break;
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home_menu, menu);

		return true;
	}

	/**
	 * Handles options menu.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);

		// Handle item selection
		switch (item.getItemId()) {
		case R.id.main_menu_brushed:
			layout.setBackgroundResource(R.drawable.home_background);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_black:
			layout.setBackgroundResource(R.drawable.home_background_port_bw);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_blue:
			layout.setBackgroundResource(R.drawable.home_background_port);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_android_metal:
			layout.setBackgroundResource(R.drawable.background3);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_android_pink:
			layout.setBackgroundResource(R.drawable.android_dude_pink);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_pink:
			layout.setBackgroundResource(R.drawable.home_background_pink);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_gold:
			layout.setBackgroundResource(R.drawable.home_background_port_gold);
			mBackground = item.getItemId();
			return true;
		case R.id.main_menu_none:
			layout.setBackgroundResource(0);
			mBackground = item.getItemId();
			return true;

		case R.id.main_menu_about:
			Dialog d = new Dialog(this);
			d.setTitle("Team F.A.T.");
			TextView tv = new TextView(this);
			tv.setText(R.string.about_dialog);
			d.setContentView(tv);
			d.show();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onStop() {
		super.onStop();

		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getPreferences(0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("background", mBackground);

		// Commit the edits!
		editor.commit();
	}
}